package com.it2297.renderer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

import com.alee.extended.image.WebDecoratedImage;
import com.alee.extended.image.WebImage;
import com.it2297.model.UsedItem;

@SuppressWarnings("serial")
public class UsedItemCellRenderer extends JPanel implements TableCellRenderer
{

	private UsedItem usedItem;

	private final JLabel lblTitle = new JLabel();
	private final JLabel lblDescription = new JLabel();
	private final JLabel lblIcon = new JLabel();
	private final JLabel lblInfo = new JLabel();

	public UsedItemCellRenderer()
	{
		setOpaque(true);
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		setLayout(new BorderLayout());

		add(lblTitle, BorderLayout.NORTH);

		lblIcon.setVerticalAlignment(JLabel.TOP);
		add(lblIcon, BorderLayout.EAST);

		lblDescription.setVerticalTextPosition(JLabel.TOP);
		lblDescription.setVerticalAlignment(JLabel.TOP);
		add(lblDescription, BorderLayout.CENTER);

		add(lblInfo, BorderLayout.SOUTH);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object object, boolean isSelected, boolean hasFocus, int row, int column)
	{
		if (object != null)
		{
			usedItem = (UsedItem) object;

			lblTitle.setText("<html><strong>" + usedItem.getTitle() + "</strong></html>");
			if (usedItem.getDescription().length() > 128)
			{
				lblDescription.setText("<html><small>" + usedItem.getDescription().substring(0, 128) + "...</small></html>");
			}
			else
			{
				lblDescription.setText("<html><small>" + usedItem.getDescription() + "</small></html>");
			}
			lblIcon.setIcon(new ImageIcon(System.getProperty("user.dir") + "/img/used-item/" + usedItem.getUuid() + "-0-thumbnail.png"));
			lblInfo.setText("<html><small>Quantity: " + usedItem.getStockCount() + "</small><br/><small>Type: " + usedItem.getType() + "</small><br/><small>Item Condition:" + usedItem.getItemCondition() + "</small><br/><small>Date Created: " + usedItem.getDateCreate() + "</small><br/><small>Donated by: " + usedItem.getDonorNRIC() + "</small><br/></html>");

			if (row % 2 == 0)
			{
				setBackground(Color.WHITE);
				if (column % 2 == 0)
				{
					Border border = BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, new Color(240, 240, 240)), BorderFactory.createEmptyBorder(10, 10, 10, 5));
					setBorder(border);
				}
				else
				{
					setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 10));
				}
			}
			else
			{
				setBackground(new Color(240, 240, 240));
				if (column % 2 == 0)
				{
					Border border = BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.WHITE), BorderFactory.createEmptyBorder(10, 10, 10, 5));
					setBorder(border);
				}
				else
				{
					setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 10));
				}
			}
			lblTitle.setForeground(Color.BLACK);
			lblDescription.setForeground(Color.BLACK);
			lblInfo.setForeground(Color.BLACK);

			if (isSelected)
			{
				lblTitle.setForeground(Color.WHITE);
				lblDescription.setForeground(Color.WHITE);
				lblInfo.setForeground(Color.WHITE);
				setBackground(new Color(80, 80, 80));
			}
		}
		else
		{
			return null;
		}

		return this;
	}
}
